package com.yjhh.tilemap.controller;

import com.yjhh.tilemap.domain.MapMarkInfo;
import com.yjhh.tilemap.services.MapMarkInfoService;
import com.yjhh.tilemap.utils.ResponseBean;
import com.yjhh.tilemap.vo.MapInfo;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.DoubleSummaryStatistics;
import java.util.List;

/**
 * Description: 提供地图所需要的接口
 *
 * @JDK Version:1.8.0_40
 * Version: V1.0
 * Author: spring
 * Date: 2017/8/11
 */
@Log4j
@RestController
@RequestMapping("/service/")
public class MapApiController {

    @Autowired
    private MapMarkInfoService mapMarkInfoService;

    @RequestMapping(value = "listAllMark", method = RequestMethod.GET)
    public ResponseBean<List<MapMarkInfo>> listAllMark() throws Exception {
        return mapMarkInfoService.listAllMark();
    }

    /**
     * @param lng 经度
     * @param lat 纬度
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "showMark/{lng}/{lat}", method = RequestMethod.GET)
    public ResponseBean<List<MapMarkInfo>> getMarkByPoint(@PathVariable("lng") Double lng, @PathVariable("lat") Double lat) throws Exception {
        return mapMarkInfoService.getMarkByPoint(lng, lat);
    }

    @RequestMapping(value = "startDrawMark", method = RequestMethod.GET)
    public ResponseBean startDrawMark() throws Exception {
        return mapMarkInfoService.startDrawMark();
    }

    /**
     * 根据缩放级别和经纬度范围查询地图瓦片信息和地图中的关键信息点
     * @param zoom     缩放级别
     * @param startLng 起始经度
     * @param startLat 起始纬度
     * @param endLng   结束经度
     * @param endLat   结束纬度
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "showMapInfo", method = RequestMethod.GET)
    public ResponseBean<MapInfo> showMapInfo(int zoom, double startLng, double startLat, double endLng, double endLat, HttpServletRequest request) throws Exception {
        return mapMarkInfoService.showMapInfo(zoom, startLng, startLat, endLng, endLat,getServerPath(request));
    }

    @RequestMapping(value = "exportMarkToHDF5", method = RequestMethod.GET)
    public void exportMarkToHDF5(HttpServletResponse response) throws IOException {
        mapMarkInfoService.exportMarkToHDF5(response);
    }

    /**
     * 获取带目录的url地址
     * @param request
     * @return
     */
    public static String getServerPath(HttpServletRequest request){
        String path = request.getContextPath();
        return request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/";
    }

}
